package com.huazai.yangge.completable.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 可重入锁 synchronized就是可重入锁
 *
 * @author huazai
 * @date 2024/2/22
 */
public class ReEntryLockDemo {
    public static void main(String[] args) {
        //Object o = new Object();
        //new Thread(() -> {
        //    System.out.println("====外层");
        //    synchronized (o) {
        //        System.out.println("====中层");
        //        synchronized (o) {
        //            System.out.println("====内层");
        //        }
        //    }
        //}).start();


        ReentrantLock lock = new ReentrantLock();
        new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "come in 外层调用");

                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName() + "come in 内层调用");
                } finally {
                    lock.unlock();
                }
            } finally {
                lock.unlock();
            }
        }, "t1").start();

        new Thread(() -> {
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "come in 外层调用");
            } finally {
                lock.unlock();
            }
        }, "t2").start();

    }
}
